package cn.schoolwow.flow.monitor.module.business.flowLog.aware;

import cn.schoolwow.ams.domain.block.common.operation.CustomerMessage;
import cn.schoolwow.ams.domain.block.common.operation.ListBlockHandlerBlockOperation;
import cn.schoolwow.ams.domain.block.common.operation.OpenFieldTabBlockOperation;
import cn.schoolwow.ams.domain.block.list.common.ListBlockButtonType;
import cn.schoolwow.ams.domain.block.list.common.ListBlockOperationHandler;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockConfigAware;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockOption;
import cn.schoolwow.ams.module.block.listBlock.common.domain.operation.BlockOperationPosition;
import cn.schoolwow.ams.module.block.listBlock.common.domain.operation.ListBlockRequest;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.flow.monitor.entity.ExceptionFlowLog;
import cn.schoolwow.flow.monitor.module.business.flowLog.flow.ExecuteFlowCommonFlow;
import cn.schoolwow.flow.monitor.module.common.domain.FlowMonitorConstant;
import cn.schoolwow.flow.monitor.module.common.flow.AddFlowCategoryNameRemoteSelectButton;
import cn.schoolwow.flow.monitor.module.common.flow.AddFlowNameRemoteSelectButton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
public class ExceptionFlowLogAware extends EntityTableListBlockConfigAware {
    @Autowired
    private FlowMonitorOption flowMonitorOption;

    @Override
    public EntityTableListBlockOption getAMSBlockConfig() {
        EntityTableListBlockOption entityTableListBlockOption = new EntityTableListBlockOption(
                FlowMonitorConstant.PROJECT_NAME,"异常流程日志", FlowMonitorConstant.DAO_NAME, ExceptionFlowLog.class);
        entityTableListBlockOption.listBlockShowOption.displayFieldNames = new String[]{
                "createdAt","flowId","name","categoryName","brokenReason","exceptionMessage","consumeTime"
        };
        entityTableListBlockOption.listBlockShowOption.initialSortField = "id";
        entityTableListBlockOption.listBlockShowOption.requestFieldNames = new String[]{
                "flowId", "name", "categoryName"
        };

        entityTableListBlockOption.listBlockShowOption.listBlockButtonTypeArray = new ListBlockButtonType[]{
                ListBlockButtonType.DELETE,
                ListBlockButtonType.MULTIPLE_DELETE,
                ListBlockButtonType.CONDITION_DELETE,
                ListBlockButtonType.ALL_DELETE,
        };
        flowMonitorOption.flowRecordQuickFlow.startFlow(new AddFlowCategoryNameRemoteSelectButton())
                .next(new AddFlowNameRemoteSelectButton())
                .putInstanceData(entityTableListBlockOption)
                .execute();
        addButtons(entityTableListBlockOption);
        return entityTableListBlockOption;
    }

    private void addButtons(EntityTableListBlockOption entityTableListBlockOption){
        ListBlockOperationHandler blockOperationHandler = new ListBlockOperationHandler() {
            @Override
            public CustomerMessage handle(ListBlockRequest listBlockRequest) throws Exception {
                flowMonitorOption.flowMonitorQuickFlow.startFlow(new ExecuteFlowCommonFlow())
                        .putData("异常流程", true)
                        .putInstanceData(listBlockRequest)
                        .execute();
                return null;
            }
        };
        {
            ListBlockHandlerBlockOperation blockOperation = new ListBlockHandlerBlockOperation("执行流程", blockOperationHandler);
            entityTableListBlockOption.blockOperationList.add(blockOperation);
        }
        {
            ListBlockHandlerBlockOperation blockOperation = new ListBlockHandlerBlockOperation("批量执行流程", blockOperationHandler);
            blockOperation.position = BlockOperationPosition.MultipleSelect;
            entityTableListBlockOption.blockOperationList.add(blockOperation);
        }
        {
            OpenFieldTabBlockOperation openFieldTabBlockOperation = new OpenFieldTabBlockOperation("查看详情",
                    Arrays.asList("flowTrace", "sql", "exception", "requestData", "contextData", "quickFlowData", "flowConfigData"));
            entityTableListBlockOption.blockOperationList.add(openFieldTabBlockOperation);
        }
    }
}
